package com.fare.taluo.core.domain.crawl.adapter.sub;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;

import com.fare.taluo.conf.ConfigContainer;
import com.fare.taluo.core.domain.crawl.CrawlCSAirLineModel;
import com.fare.taluo.core.domain.crawl.CrawlTaskModel;
import com.fare.taluo.core.domain.crawl.adapter.AbstractAdapter;
import com.fare.taluo.util.DateUtils;
/**
 * 
 * @author zengxiangqi/13640225259
 *
 */
public class ZHAirLinePdfAdaptor extends AbstractAdapter {
	public ZHAirLinePdfAdaptor(CrawlTaskModel taskModel) {
		super(taskModel);
		// TODO Auto-generated constructor stub
	}
	private HttpClient client = null;
	
	
	@Override
	public List<Object> paraseToVo(Object fetchObject) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getUrl() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object fetch(String url) throws Exception {
		return this.fetchURI(url);
	}

	@Override
	public boolean validateFetch(Object fetchObject) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}
	
	private File fetchURI(String uri) throws Exception {
		HttpPost request;
		HttpEntity entity;
		byte data[];
		request = null;
		HttpResponse response = null;
		entity = null;
		InputStream is = null;
		data = null;
		String s;
		try {
			if (client == null)
				client = super.getHttpClient();
			client.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,1000000);
		        //响应超时
			client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 1000000);
			request = new HttpPost(uri);
			response = client.execute(request);
			entity = response.getEntity();
			if (entity != null) {
				is = entity.getContent();
				return writeFile(is);
			}

		    
		} finally {
			if (is != null)
				try {
					is.close();
				} catch (Exception e) {
				}
			;
			is = null;
			if (request != null)
				request.releaseConnection();
			if (entity != null)
				EntityUtils.consume(entity);
			entity = null;
			response = null;
			request = null;
		}
		return null;
	}
	
	/**
	 * 保存pdf
	 * @param is
	 * @return
	 * @throws IOException
	 */
	protected File writeFile(InputStream is) throws IOException {
		FileOutputStream fos = null;
		BufferedInputStream bis = null;
		int BUFFER_SIZE = 1024;
		byte[] buf = new byte[BUFFER_SIZE];
		int size = 0;
		bis = new BufferedInputStream(is); 
        //建立文件 
		File file = new File(DateUtils.dateToStr(new Date(),
				"yyyy-MM-dd")+"-ZH.pdf");
	    fos = new FileOutputStream(file);   
	    //保存文件   
	    while ( (size = bis.read(buf)) != -1)    
	    { fos.write(buf, 0, size); }  
	 
	    fos.close();   
	    bis.close();
		return file;
	}

	
    public static void main(String[] args) {
		try {
			ConfigContainer.getInstance().register("com.fare.taluo.core.domain.crawl.CrawlPropertiesLoader");
			new ZHAirLinePdfAdaptor(null).fetchURI(null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
